<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
    <meta charset="UTF-8">
    <title>钥匙详情</title>
    {% include 'template/baseCss.volt' %}
    <link rel="stylesheet" href="/css/vendor/cmp-controls.css" type="text/css">
    <link href="/css/vendor/add-drone-group.less" rel="stylesheet/less" type="text/css">
    <script src="/js/less.min.js"></script>

</head>

<body>

<main>
    <div class="info-group">
        <div class="title-bar">
            <label class="server-id">钥匙详情</label>
            <div style="float: right;" class="button" onClick="DataProxy.clickBack()">
                <label class="btn-text">返回</label>
                <img class="del" src="/images/back.png">
            </div>

            <div style="float: right;" class="button" id="edit-btn" onClick="DataProxy.clickEdit()">
                <label class="btn-text" id="btn-edit">编辑</label>
                <img src="/images/info_edit_edit.png">
            </div>
        </div>

        <div class="info-wrap">
            <table >
                <tr>
                    <th>ID:</th>
                    <td><input type="text" disabled="disabled" data-name="id">
                        <div class="history button"></div>
                    </td>
                </tr>

                <tr>
                    <th>钥匙ID:</th>
                    <td><input data-name="key_id"  data-valid="presence" type="text"
                               disabled="disabled">
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>证书类型:</th>
                    <td><select data-name="type" data-edit="true" disabled="disabled">
                            <option value="">请选择</option>
                            <option value="1">混合证书</option>
                            <option value="0">无限期</option>
                        </select></td>
                </tr>
                <tr>
                    <th>试用过期时间:</th>
                    <td><input type="text" data-attr="date" data-edit="true" data-name="expired_at"
                               disabled="disabled"></td>
                </tr>
                <tr>
                    <th>试用里程:</th>
                    <td><input data-name="kilometer"  data-valid="presence" type="text" data-edit="true"
                               disabled="disabled">

                    </td>
                </tr>
                <tr>
                    <th>是否投保第三方险:</th>
                    <td><select data-name="insurance" data-edit="true" disabled="disabled">
                            <option value="">请选择</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>第三方险结束时间:</th>
                    <td><input type="text" data-attr="date" data-edit="true" data-name="insurance_end_at"
                               disabled="disabled">
                        <div class="history button"></div>
                    </td>
                </tr>


                <tr>
                    <th>持有单位:</th>
                    <td><input data-name="unit_name"  data-valid="presence" type="text" data-edit="true"
                               disabled="disabled">
                    </td>
                </tr>
                <tr>
                    <th>联系人:</th>
                    <td><input data-name="contact_name"  data-valid="presence" type="text" data-edit="true"
                               disabled="disabled">
                    </td>
                </tr>
                <tr>
                    <th>备注:</th>
                    <td><input data-name="remark"  data-valid="presence" type="text" data-edit="true"
                               disabled="disabled">
                    </td>
                </tr>
            </table>
            <table >
                <tr>
                    <th>钥匙类型:</th>
                    <td><input type="text" disabled="disabled" data-name="plane_model_name" >
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>证书:</th>
                    <td><textarea data-name="key_data"  data-valid="presence" type="text"
                                  disabled="disabled">
                            </textarea>
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>证书更新时间:</th>
                    <td><input type="text" data-attr="date" data-name="created_at"
                               disabled="disabled"></td>
                </tr>
                <tr>
                    <th>起始公里数:</th>
                    <td><input type="text" data-edit="true" data-name="begin_kilometer" disabled="disabled"></td>
                </tr>
                <tr>
                    <th>图像处理:</th>
                    <td><select data-name="smart_map" data-edit="true" disabled="disabled">
                            <option value="">请选择</option>
                            <option value="1">可用</option>
                            <option value="0">不可用</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>第三方险开始时间:</th>
                    <td><input type="text" data-attr="date" data-edit="true" data-name="insurance_begin_at"
                               disabled="disabled">
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>飞机编号:</th>
                    <td>
                        <input data-name="uas_serial_id" type="text" data-valid="presence" data-edit="true"
                               disabled="disabled" onclick="DataProxy.clickChangePlane()">
                        <div class="history button"></div>
                    </td>
                </tr>
                <tr>
                    <th>联系地址:</th>
                    <td><input data-name="contact_addr"  data-valid="presence" type="text" data-edit="true"
                               disabled="disabled">
                    </td>
                </tr>
                <tr>
                    <th>手机:</th>
                    <td><input data-name="phone"  data-valid="phone" type="text" data-edit="true"
                               disabled="disabled">
                    </td>
                </tr>

            </table>
        </div>
    </div>

    <div class="save-btn-wrap">
        <p id="save-btn" class="button" onClick="DataProxy.clickSave()">保 存</p>
    </div>
</main>

</body>

{% include 'template/baseJs.volt' %}
<script src="/js/vendor/add-drone-group.js"></script>
<script>

    //孩子回调回来的参数
    function childrenCallBack(param) {
        var uid = param.replace("uid=", "");
        $("#page-mask-container").remove();

        $("[data-name=uas_serial_id]").val(uid);
    }


    var DataProxy = {
        editState: false,
        keyId: false,
        prePlaneData: false,
        preKeyData: false,
        init: function () {
            DataAttrBinder.init();
            DataValidBinder.bind();
            this.keyId = getQueryString("id");
            this.requestDetail();
        },
        clickChangePlane: function () {

            $("body").append("<div id='page-mask-container' class='page-mask-container'>" +
                    "<iframe class='center-vh' style='width: 80%; height: 90%; border: none;' src='/plane/list?isGet=true'>" +
                    "</iframe></div>");
            cmpControlM.pageMaskContainerListenEvent();
        },
        clickSave: function () {

            if (DataValidBinder.valid()) {
                //点击保存
                var param = new Object();
                $("[data-edit=true]").each(function () {
                    var item = $(this);
                    var name = item.attr("data-name");
                    var value = item.val();
                    if (value && value != "") {
                        param[name] = value;
                    }
                });
                param['action_key'] = "ACTION_UPDATE";
                param['id'] = $("[data-name=id]").val();
                param["key_id"]=$("[data-name=key_id]").val();
                feimaPost("/plane/detailKey", param, function (result) {
                    FAlert("更新成功", function () {
                        DataProxy._updateButtonState();
                        DataProxy.requestDetail();
                    });

                });
            }


        },
        clickBack: function () {
            if (this.editState) {
                FConfirm("您确定放弃编辑吗?", function (result) {
                    if (result) {
                        DataProxy._goBack();
                    }
                });
            } else {
                DataProxy._goBack();
            }
        },
        _goBack: function () {
            var pre = encodeURI(getQueryString("prefilter"));
            var from = getQueryString("from");
            window.location.href = from + "?prefilter=" + pre;
            console.info(his);
        },
        clickEdit: function () {

            if (this.editState) {
                FConfirm("您确定放弃编辑吗?", function (result) {
                    if (result) {

                        DataProxy._updateButtonState();
                        DataProxy.updatePageData();
                    }
                });
            } else {
                //取消编辑
                this._updateButtonState();
            }
        },
        requestDetail: function () {
            feimaPost("/plane/detailKey", {
                "action_key": "ACTION_DETAIL",
                "id": this.keyId
            }, function (result) {
                if (result) {
                    DataProxy.preKeyData = result.data;
                    DataProxy.updatePageData();
                }
            });
        },
        updatePageData: function () {
            if (DataProxy.preKeyData) {
                this._updateData(DataProxy.preKeyData, false);
            }
        },
        _updateData: function (data, preStr) {
            for (var item in data) {
                var value = data[item] ? data[item] : "";
                item = preStr ? (preStr + item) : item;
                //针对D1000特殊化处理
                if(item=="plane_model_name"){
                    if(value.indexOf("Phantom")>=0||
                            value=="D1000"
                    ){
                        $("[data-name=kilometer]").removeAttr("data-edit");
                    }
                }
                if (this._bindValue(item, value)) {
                    continue;
                }
            }
        },
        _bindValue: function (item, value) {
            var jSelector = $("[data-name=" + item + "]");
            if (jSelector.is("input")
                    || jSelector.is("select")
            ) {
                jSelector.val(value);
                return true;
            }
            if(jSelector.is("textarea")){
                jSelector.text(value);
                jSelector.val(value);
                return true;
            }


            return false;
        },
        _updateButtonState: function () {
            this.editState = !this.editState;
            $("#btn-edit").text(this.editState ? "取消编辑" : "编辑");
            hCommon.setElementEnable($("[data-edit=true]"), this.editState);
            hCommon.showElement($("#save-btn"), this.editState);
            hCommon.showElement($("#btn-change"), this.editState);

        }

    };

    $(function () {

        DataProxy.init();

    });

    $(".history").bind("click", function () {

        var pre = $(this).prev();
        var uas = $("#uas_serial_id").val();
        if (pre.is("input") || pre.is("select")) {
            //
        } else {
            pre = pre.prev();
        }
        var name = pre.attr("data-name");

        var title = $(this).parent().prev().text();
        title = title.replace(":", "");
        //请求当前的历史记录

        feimaPost("/plane/doListHistory", {
            "name": name,
            "uas_serial_id": uas
        }, function (result) {

            if (result.status == 0) {
                var data = result.data;
                var viewData = new Array();
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    var user = item["phone"] + "(" + item["nickname"] + ")";
                    viewData.push({
                        "content": item["operation_content"],
                        "user": user,
                        "time": item["created_at"]
                    });
                }

                var historyInfo = {
                    "title": title,
                    "items": viewData
                };
                addDroneEditor.showHistory(historyInfo);
            }

        });


    });
</script>

</html>






